package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.enums.CancelOrderTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.req.ConfirmApplyRefundReq;
import com.shop2cn.iapi.sqoptrade.parameter.req.GetRefundMaxAmountsReq;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.GetRefundMaxAmountsCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.RefundService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 退货退款申请效验接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqGetRefundMaxAmounts {

	private static GetRefundMaxAmountsReq validateRefundReq;
	private static GetRefundMaxAmountsCall applyvalidationCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId")); 

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("神器退货退款申请校验接口");
	}

	@Before
	public void caseUp() {
		validateRefundReq = new GetRefundMaxAmountsReq();
		applyvalidationCall = new GetRefundMaxAmountsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applyvalidationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 整单退款
	 * @param orderId
	 */
	public void verify (long orderId) throws JSONException {
		
		BigDecimal totalProductRefundAmount = BigDecimal.ZERO;
		BigDecimal totalTaxRefundAmount = BigDecimal.ZERO;
		BigDecimal totalFreightRefundAmount = BigDecimal.ZERO;
		
		List<OrderDetailPo> OrderDetailPos = orderShardingWapper.selectOrderDetailbyOrderId(orderId);
		
		for (OrderDetailPo orderDetailPo : OrderDetailPos) {
			
			int refundCount = orderDetailPo.getProductCount();
			
			BigDecimal productRefundAmount = RefundService.getOrderDetailPaidAmountExcludeFreight(
					orderDetailPo, refundCount, true);
			
			BigDecimal freightRefundAmount = RefundService.getMaxCanRefundFreight(orderDetailPo,
					orderDetailPo.getFreight());
			
			BigDecimal taxRefundAmount = RefundService.getOrderDetailTaxFeeAmount(
					orderDetailPo, refundCount);
			
			totalProductRefundAmount = totalProductRefundAmount.add(productRefundAmount);
			totalFreightRefundAmount = totalFreightRefundAmount.add(freightRefundAmount);
			totalTaxRefundAmount = totalTaxRefundAmount.add(taxRefundAmount);
		}
		
		Logger.verifyEquals("true", applyvalidationCall.getString("success"),
				"验证返回码");
		
		JSONObject object = new JSONObject(applyvalidationCall.getReturnData());

		DataUtil.verifyDecimalEquals(totalProductRefundAmount, new BigDecimal(
				object.optString("productMaxRefundAmount")),
				"验证productRefundAmount");

		DataUtil.verifyDecimalEquals(totalTaxRefundAmount, new BigDecimal(
				object.optString("taxMaxRefundAmount")),
				"验证taxRefundAmount");
		
		DataUtil.verifyDecimalEquals(totalFreightRefundAmount, new BigDecimal(
				object.optString("freightMaxRefundAmount")),
				"验证freightRefundAmount");
	}
	
	/**
	 * 
	 * @param order
	 * @param orderDetailPo
	 * @param refundCount
	 */
	private void verify(long orderId, String catalogId, Integer refundCount) throws Exception {

		BigDecimal productRefundAmount = BigDecimal.ZERO;
		BigDecimal taxRefundAmount = BigDecimal.ZERO;
		BigDecimal freightRefundAmount = BigDecimal.ZERO;
		
		OrderDetailPo orderDetailPo = orderShardingWapper
				.selectOrderDetailbyOrderIdAndCatalogId(orderId, catalogId);
		
		// 整单退款
		if (null == refundCount) {
			refundCount = orderDetailPo.getProductCount();
			
			productRefundAmount = RefundService.getOrderDetailPaidAmountExcludeFreight(
					orderDetailPo, refundCount, true);
		} else
			productRefundAmount = RefundService.getOrderDetailPaidAmountExcludeFreight(
					orderDetailPo, refundCount, false);
		
		freightRefundAmount = RefundService.getMaxCanRefundFreight(orderDetailPo,
				orderDetailPo.getFreight());
		
		taxRefundAmount = RefundService.getOrderDetailTaxFeeAmount(
				orderDetailPo, refundCount);

		Logger.verifyEquals("true", applyvalidationCall.getString("success"),
				"验证返回码");
		
		JSONObject object = new JSONObject(applyvalidationCall.getReturnData());

		DataUtil.verifyDecimalEquals(productRefundAmount, new BigDecimal(
				object.optString("productMaxRefundAmount")),
				"验证productRefundAmount");

		DataUtil.verifyDecimalEquals(taxRefundAmount, new BigDecimal(
				object.optString("taxMaxRefundAmount")),
				"验证taxRefundAmount");
		
		DataUtil.verifyDecimalEquals(freightRefundAmount, new BigDecimal(
				object.optString("freightMaxRefundAmount")),
				"验证freightRefundAmount");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_001() {
		Logger.start(true, "用户下单后申请效验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			
			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_ORDER_CANCELLED",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("当前状态不能退货退款", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_002() {
		Logger.start(true, "用户支付后接单前效验成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);
			
			Order order = placeOrderResp.getOrderList().get(0);
			
			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(1);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_003() {
		Logger.start(true, "商家接单前取消订单,退货退款失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);
			
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.cancelOrder(orderId, CancelOrderTypeEnum.SELLER_CANCEL_ORDER, autotest_goodsOwnerId);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_ORDER_CANCELLED",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("当前状态不能退货退款", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_004() {
		Logger.start(true, "商家接单后,带有各种优惠，整单退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithAllPromotion(productBean, new BigDecimal("7.00"), autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId);

		} catch (Exception e) {

			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_005() {
		Logger.start(true, "用户收货之后申请校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, 2);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_006() {
		Logger.start(false, "订单号不存在申请效验");
		try {

			validateRefundReq.setOrderId(111111L);
			validateRefundReq.setCatalogId("c123456");
			validateRefundReq.setProductCount(1);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ErrOrderNotExists",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("订单不存在", validateRefundReq.getOrderId()),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_007() {
		Logger.start(false, "订单号为null申请效验");
		try {
			// validateRefundReq.setOrderId(111111);
			validateRefundReq.setCatalogId("c123456");
			validateRefundReq.setProductCount(1);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(
					String.format("orderId:must be greater than or equal to 1",
							validateRefundReq.getOrderId()),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_008() {
		Logger.start(true, "商家发货后,退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(3000);

			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, 2);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_010() {
		Logger.start(true, "退款商品数量大于实际数量");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount() + 1);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("超过商品的最大可退数量，无法退款", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_011() {
		Logger.start(true, "盛付通支付不能发起部分退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(1);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_012() {
		Logger.start(true, "盛付通支付发起整单退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_013() {
		Logger.start(true, "主单拆单后的订单，盛付通支付不能发起全部退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder2(BigDecimal.TEN, productBean, 2);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_ORDER_CANCELLED",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("抱歉，该订单因合并支付无法退款", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_014() {
		Logger.start(true, "店主不能发起退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			Long userId = (long) new OrderCallService().createNewUser();

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserId(productBean, userId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(productBean.getSellerId());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_NO_PERMISSION",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("没有权限发起退款申请", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_015() {
		Logger.start(true, "支持线下付款订单,但是为线上支付，可以发起退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false, false);
			String currency = productBean.getCurrency();
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO, autotest_goodsOwnerId, currency, true);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_016() {
		Logger.start(true, "已线下付款的订单不能发起退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false, false);
			String currency = productBean.getCurrency();
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO, autotest_goodsOwnerId, currency, true);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			// 支付线下支付订单
			SqTradingCallService.payOrder(userId, placeOrderResp.getTradingId(), "wxsqBuyer", "200");
			SqTradingCallService.confirmOfflinePay(orderId, autotest_goodsOwnerId);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ERR_STATUS_NOTMATCH",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("线下支付订单，不能退货退款", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_017() {
		Logger.start(true, "第一个商品已经退了全部运费，满减包邮的第二个商品发起退款验证");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithAllPromotion(productBean, BigDecimal.ZERO, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");
			
			String refundedCatalogId = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(1).getCatalogId();
			
			HashMap<String, BigDecimal> map = RefundService
					.getShenqiRefundAmount(orderId, autotest_goodsOwnerId,
							refundedCatalogId, 1);
			
			ConfirmApplyRefundReq req = RefundService.buildStandApplyRefundReq(orderId,
					autotest_goodsOwnerId, refundedCatalogId,
					map.get("maxRefundFreight").subtract(BigDecimal.ONE), map.get("maxRefundTax"),
					map.get("maxProductRefundAmount"), 1);
			
			RefundService.applyRefundCall(req);

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_018() {
		Logger.start(true, "第一个商品已经退了全部运费，第二个商品发起退款验证");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrder(productBean, new BigDecimal("0.17"));

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");
			
			String refundedCatalogId = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(1).getCatalogId();
			
			HashMap<String, BigDecimal> map = RefundService
					.getShenqiRefundAmount(orderId, autotest_goodsOwnerId,
							refundedCatalogId, 1);
			
			ConfirmApplyRefundReq req = RefundService.buildStandApplyRefundReq(orderId,
					autotest_goodsOwnerId, refundedCatalogId,
					map.get("maxRefundFreight").subtract(BigDecimal.ONE), map.get("maxRefundTax"),
					map.get("maxProductRefundAmount"), 1);
			
			RefundService.applyRefundCall(req);

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(orderDetailPo.getProductCount());
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_019() {
		Logger.start(true, "商家接单后,带有各种优惠，部分退货退款校验");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithAllPromotion(productBean, new BigDecimal("7.00"), autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(1);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(1);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {

			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_020() {
		Logger.start(true, "虚拟库存订单，单个商品无法部分退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			Order order = placeOrderResp.getOrderList().get(0);
			
			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setCatalogId(orderDetailPo.getCatalogId());
			validateRefundReq.setProductCount(1);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			Logger.verifyEquals("false",
					applyvalidationCall.getString("success"), "验证返回码");

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					applyvalidationCall.getString("errorCode"), "验证返回码");

			Logger.verifyEquals(String.format("虚拟库存商品只能退所有商品", orderId),
					applyvalidationCall.getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_021() {
		Logger.start(true, "带有服务费率的订单进行整单退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithServiceRate(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetRefundMaxAmounts_022() {
		Logger.start(true, "带有服务费率的订单进行部分退款");

		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithServiceRate(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			Thread.sleep(3000);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);
			
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			OrderDetailPo orderDetailPo = placeOrderResp.getOrderList().get(0)
					.getOrderDetailPoList().get(0);

			validateRefundReq.setOrderId(orderId);
			validateRefundReq.setSellerId(autotest_goodsOwnerId);
			validateRefundReq.setCatalogId(order.getOrderDetailPoList().get(0).getCatalogId());
			validateRefundReq.setProductCount(order.getOrderDetailPoList().get(0).getProductCount());

			applyvalidationCall.setData(validateRefundReq);
			applyvalidationCall.callService();

			verify(orderId, orderDetailPo.getCatalogId(),
					validateRefundReq.getProductCount());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
